package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.tbit.uqbike.client.dao.core.RechargeConfigDao;
import com.tbit.uqbike.client.pojo.AddCountFee;
import com.tbit.uqbike.client.pojo.RechargeConfig;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.vo.RechargeConfigVO;
import com.tbit.uqbike.client.service.AddCountFeeService;
import com.tbit.uqbike.client.service.RechargeConfigService;
import com.tbit.uqbike.client.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description: 充值界面金额配置
 * @Date: 创建于 13:51 2020/5/8
 */
@Service
public class RechargeConfigServiceImpl implements RechargeConfigService {
    @Autowired
    private RechargeConfigDao rechargeConfigDao;
    @Autowired
    private UserService userService;
    @Autowired
    private AddCountFeeService addCountFeeService;

    @Override
    public List<RechargeConfig> getByAccountId(Integer accountId) {
        return rechargeConfigDao.getByAccountId(accountId);
    }

    @Override
    public List<RechargeConfigVO> getRechargeInfoByAccountId(Integer accountId, Integer userId, int minMoney) {
        List<RechargeConfigVO> list = rechargeConfigDao.getRechargeInfoByAccountId(accountId);
        if (userId == null){
            return list;
        }
        User user = userService.getByUserId(userId);
        Double money = user.getMoney();
        AddCountFee fee = addCountFeeService.getFee(accountId);
        Integer recommendMoney = 0;
        if(fee != null){
            recommendMoney = (int) (fee.getRechargeBase() - money);
        }
        if (recommendMoney > 0) {
            RechargeConfigVO rechargeConfigVO = new RechargeConfigVO();
            rechargeConfigVO.setMoney(recommendMoney);
            rechargeConfigVO.setBaseMoney(recommendMoney);
            rechargeConfigVO.setGiveMoney(0);
            rechargeConfigVO.setRemark("推荐充值金额");
            rechargeConfigVO.setTip("推荐充值");
            rechargeConfigVO.setType("0");
            list.add(0, rechargeConfigVO);
        }
        for (RechargeConfigVO rechargeConfigVO : list) {
            String remark = rechargeConfigVO.getRemark();
            if (!StringUtils.isEmpty(remark)) {
                boolean isGiveMoney = rechargeConfigVO.getGiveMoney() != null && rechargeConfigVO.getGiveMoney() > 0;
                boolean isGiveCoupon = rechargeConfigVO.getCouponId() != null && rechargeConfigVO.getCouponId() > 0;
                if (isGiveMoney) {
                    rechargeConfigVO.setType("2");
                    rechargeConfigVO.setTip("赠" + (rechargeConfigVO.getGiveMoney()/100.0) + "元");
                }
                if (isGiveCoupon) {
                    rechargeConfigVO.setType("3");
                    rechargeConfigVO.setTip("赠优惠券");
                }
                if (isGiveMoney && isGiveCoupon) {
                    rechargeConfigVO.setType("4");
                    rechargeConfigVO.setTip("充值有礼");
                }
            }
        }
        if (minMoney > 0) {
            // 过滤一下不符合最小金额的
            list = list.stream()
                    .filter(item -> {
                        // 过滤掉自定义金额
                        if (item.getMoney() < 0) {
                            return false;
                        }
                        return item.getMoney() + ObjUtil.defaultIfNull(item.getGiveMoney(), 0)
                                > minMoney;
                    })
                    .collect(Collectors.toList());

            // 没有符合的，生成一个
            if (list.isEmpty()) {
                RechargeConfigVO genConfig = new RechargeConfigVO();
                genConfig.setMoney(minMoney + 100);
                genConfig.setType("1");
                list.add(genConfig);
            }
        }

        return list;
    }
}
